﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class a4_购物车_ : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataClassesDataContext a1 = new DataClassesDataContext();
        int? i;
        i = a1.login((String)Session["phone"], (String)Session["phone"], (String)Session["psw"]);
        if (i == 0)
        {
            Response.Write("<script>alert('请先登录！');location='homepage.aspx'</script>");
            //Response.Redirect("homepage.aspx");
        }

    }
  
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
   
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gr in GridView1.Rows)
        {
            CheckBox c1 = (CheckBox)gr.FindControl("CheckBox1");
            if (c1.Checked)
            {
                Label sign = (Label)gr.FindControl("Label1");
                TextBox num2 = (TextBox)gr.Cells[1].FindControl("TextBox1");
                //Response.Write(xh);
                int num1 = Convert.ToInt32(num2.Text);
                DataClassesDataContext a1 = new DataClassesDataContext();
                int? w;
                w=a1.select_inventory(sign.Text);
                if (w > num1)
                {
                    a1.update_inventory(sign.Text, num1);
                    //Response.Write(sign.Text + "," + num1);
                    a1.delete_cart((String)Session["phone"], sign.Text);
                    Response.Write("<script>alert('购买成功！');location='a4(购物车).aspx'</script>");
                }
                else { Response.Redirect("<script>alert('库存不足，请修改购买量！')</script>"); }
            }
        }
    }
    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            System.Web.UI.WebControls.CheckBox checkbox = (System.Web.UI.WebControls.CheckBox)GridView1.Rows[i].FindControl("checkbox1");
            if (CheckBox2.Checked == true)
            {
                checkbox.Checked = true;
            }
            else
            {
                checkbox.Checked = false;
            }
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gr in GridView1.Rows)
        {
            CheckBox c1 = (CheckBox)gr.FindControl("CheckBox1");
            if (c1.Checked)
            {
                Label sign = (Label)gr.FindControl("Label1");
                DataClassesDataContext a1 = new DataClassesDataContext();
                a1.delete_cart((String)Session["phone"], sign.Text);
            }
            
        }
        Response.Write("<script>alert('删除成功！');location='a4(购物车).aspx'</script>");
        //GridView1.DataBind();
        
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gr in GridView1.Rows)
        {
            
            
                Label sign = (Label)gr.FindControl("Label1");
                TextBox num2 = (TextBox)gr.Cells[1].FindControl("TextBox1");
                int num1 = Convert.ToInt32(num2.Text);
                DataClassesDataContext a1 = new DataClassesDataContext();
                a1.update_cart1((String)Session["phone"],sign.Text, num1);
                //Response.Write(num1);
                
        }
        Response.Write("<script>alert('信息已更新！');location='a4(购物车).aspx'</script>");
    }
  
}